package com.yonyou.ucf.mdf.app.isvdxq.other.service;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.type.TypeReference;
import com.yonyou.ucf.mdf.app.isvdxq.common.ApiYon;
import com.yonyou.ucf.mdf.app.isvdxq.other.mapper.impl.UnitassistMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.yonyisv.model.EventContent;
import com.yonyou.ucf.mdf.app.isvdxq.other.entity.UnitassistEntity;
import com.yonyou.ucf.mdf.app.isvdxq.other.model.*;
import com.yonyou.ucf.mdf.app.isvdxq.utils.GenericResponse;
import com.yonyou.ucf.mdf.app.isvdxq.utils.RequestTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class UnitassistService {
    @Autowired
    private BaseService baseService;
    @Autowired
    private UnitassistMapperImpl unitassistMapper;

    public UnitassistEntity SelectById(Long Id) {
        return unitassistMapper.selectById(Id);
    }

    public Boolean CheckEntity(Long Id) {
        int rInt = unitassistMapper.selectCount(Id);
        if (rInt > 0) return true;
        else return false;
    }

    public Integer InsertEntity(UnitassistEntity entity) {
        return unitassistMapper.insert(entity);
    }

    public Integer UpdateEntity(UnitassistEntity entity) {
        return unitassistMapper.updateByPrimaryKey(entity);
    }

    public List<UnitassistEntityBean> SelectAll(String tenantId) {
        return unitassistMapper.selectAll(tenantId);
    }
    public List<UnitassistEntityBean> SelectByProId(String proId, String tenantId) {
        return unitassistMapper.selectAll(tenantId);
    }

    public Integer DeleteById(Long Id) {
        return unitassistMapper.deleteByPrimaryKey(Id);
    }


    //新增并更新
    public Integer InsertBatch(List<UnitassistEntity> list) {
        if (list.size() == 0) return 0;
        long start = System.currentTimeMillis();
        int res = unitassistMapper.insertBatch(list);
        long end = System.currentTimeMillis();
        System.out.println("---------------" + (start - end) + "---------------");
        return res;
    }

    //模板单位数据加载同步
    public void EventProductUnitTempEdit(String productApplyRangeId, String detailId, String tenantId) throws IOException {
        AccessTokenResponse MyToken = baseService.getAccessToken3(tenantId);
        String token = MyToken.getAccessToken();
        String gatewayUrl = baseService.buildBusinessUrl(tenantId);

        String requrl = gatewayUrl + ApiYon.getProtplDetail;
        Map<String, String> params = new HashMap<>();
        if (detailId.equals("")) return;
        // 除签名外的其他参数
        params.put("access_token", token);
        params.put("id", detailId);
        //params.put("productApplyRangeId", productApplyRangeId);
        String resString = RequestTool.doGet(requrl, params);
        GenericResponse<ProductBeanForName<NameBean>> response = RequestTool.getBean(resString, new TypeReference<GenericResponse<ProductBeanForName<NameBean>>>() {
        });

        List<UnitassistEntity> myDataUnit = new ArrayList<>();
        if (response.getCode().equals("200")) {
            myDataUnit = response.getData().getProductAssistUnitExchanges();
            if (myDataUnit != null && myDataUnit.size() > 0) {
                myDataUnit.forEach(item -> {
                    item.setProductTemplate(detailId);
                    item.setTenant_id(tenantId);
                });
                int rIntNew = InsertBatch(myDataUnit);
                System.out.println("EventProductUnitTempEdit影响条数" + rIntNew);
            }
        }
    }

    public Integer EventEdit(EventContent content) throws IOException {
        System.out.println(content.getContent());
        ContentBean obj = RequestTool.getBean(content.getContent(), ContentBean.class);
        String jsonString = obj.getArchive().replace("detail!id", "detailid").replace("detail!", "");
        UnitassistEntity unitEnity = RequestTool.getBean(jsonString, UnitassistEntity.class);
        unitEnity.setTenant_id(obj.getTenantId());
        System.out.println(JSON.toJSONString(unitEnity));
        Boolean flag = CheckEntity(unitEnity.getId());
        if (flag) {
            int rInt = UpdateEntity(unitEnity);
            return rInt;
        } else {
            int rInt = InsertEntity(unitEnity);
            return rInt;
        }
    }

    public Integer EventDel(EventContent content) throws IOException {
        System.out.println(content.getContent());
        ContentBean obj = RequestTool.getBean(content.getContent(), ContentBean.class);
        Long Id = Long.parseLong(obj.getId());
        Boolean flag = CheckEntity(Id);
        if (flag) {
            int rInt = DeleteById(Id);
            return rInt;
        }
        return 0;
    }
}
